(:
    Extraction of information from the Drew Grapher tool
 :)

import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
declare boundary-space strip;


(: Modifiable color for texts : set a color or "" for no color :)
declare variable $evcolor := "darkred" ;
declare variable $evbgcolor := "" ;


(:
   Recherche de l'indice d'une chaine dans une liste de chaines
   now unused...
 :)
declare function local:cvn($itl as xs:string*, $elt as xs:string) as xs:int
{
    for $e at $p in $itl
    where $e = $elt
    return $p
}

(:
  Quelques variables globales
 :)
declare variable $docname := $arguments[1] ;   (: Name of initial trace file :)
declare variable $gel := doc($docname)/trace/event[grapheur] ;  (: all grapher events :)

declare variable $colorattr :=
  (  if ($evcolor != "") then attribute color { $evcolor } else () ,
     if ($evbgcolor != "") then attribute bgcolor { $evbgcolor } else ()  ) ;

(:
  La liste des identifications de boites
:)
declare variable $gec := $gel[grapheur/argument[@action="create"]];
declare variable $boxes := 
   for $e in $gec
   where $e[grapheur/argument[@type="box"]]
   return
     string($e/grapheur/argument/@id) ;
(:
  La liste des identifications de cercles
:)
declare variable $arrows := 
   for $e in $gec
   where $e[grapheur/argument[@type="arrow"]]
   return
     string($e/grapheur/argument/@id) ;

(:declare function local:objid($elt as xs:string) as xs:string
{
    concat ((for $e at $p in $boxes where $e = $elt return ("la boite ", $elt)),
    (for $e at $p in $arrows where $e = $elt return ("la fleche ", $elt))  )
}:)

declare function local:objtype($elt as xs:string) as xs:string
{
    if ($boxes = ($elt)) 
	then "box"
  	else "arrow"
}

declare function local:objid($elt as xs:string) as xs:string
{
    if ($boxes = ($elt)) 
	then concat ("la boite ", $elt)
  	else concat ("la fleche ", $elt)
}

(:  An error function  :)
declare function local:erreur($msg as xs:string, $itm as node()) as node()*
{
    <info name="error">{$msg}</info>,
    <info name="content"><text color="red">{
        ($msg , $itm/grapheur/(argument|relation)/*/name()) }</text></info>
}


(:
    Calcul effectif du resultat
 :)
<display>{
for $e at $p in $gel
where $e[grapheur]
return
	if ($e/grapheur/relation and fn:not($e/grapheur/argument) and ($e/time/date = $e/preceding-sibling::*[1]/time/date)) then
		()
  else
  <item>{
    <info name="type">grapher</info> ,
    <info name="time">{ $e/time }</info>,
    <info name="src-anchor">{
      <anchor>{
        <doc>{ $docname }</doc>,
        <path>{jj:build-Path($e)}</path>
      }</anchor>
    }</info>,
    <info name="user">{string($e/@user)}</info>,
    <info name="room">{string($e/@room)}</info>,
    if ($e/grapheur/engaged) then
       if ($e/grapheur/engaged[@status="false"]) then
          ( <info name="engaged">false</info>,
            <info name="op">disengage</info>,
            <info name="content"><text>{ $colorattr, "se d&#233;connecte du grapheur"}</text></info>) 
       else 
          ( <info name="engaged">true</info>,
            <info name="op">engage</info>,
            <info name="content"><text>{ $colorattr, "se connecte au grapheur"}</text></info>)
    else
    if ($e/grapheur/argument) then
        (
             <info name="graphid">{string($e/grapheur/argument/@id)}</info>,
             if ($e/grapheur/argument[@action="create"] and $e/grapheur/argument[@type="box"]) then
                 ( <info name="op">create box</info>,
                   <info name="opx">box</info>,
                   <info name="content"><text>{ $colorattr, "cr&#233;e",
                       local:objid(string($e/grapheur/argument/@id))}</text></info> )
             else
             if ($e/grapheur/argument[@action="create"] and $e/grapheur/argument[@type="arrow"]) then
            		 ( <info name="op">create arrow</info>,
                   <info name="opx">arrow</info>,
                   <info name="content"><text>{ $colorattr, "cr&#233;e",
                       local:objid(string($e/grapheur/argument/@id)), 
                       if ($e/following-sibling::*[1]/grapheur/relation) then
                       			("de",local:objid(string($e/following-sibling::*[1]/
                       															grapheur/relation[1]/@from)))
                       else if (fn:count($e/grapheur/relation)>1) then
                            ("de",local:objid(string($e/grapheur/relation[2]/@to)))
                       else (),
                       if ($e/grapheur/relation) then
                       			("vers",local:objid(string($e/grapheur/relation[1]/@to)))
                       else ()
                       			}</text></info> )
             else
             if ($e/grapheur/argument[@action="delete"]) then
                 ( <info name="op">{("delete", local:objtype(string($e/grapheur/argument/@id)))}</info>,
                   <info name="content"><text>{ $colorattr, "efface",
                       local:objid(string($e/grapheur/argument/@id))}</text></info> )
             else
             if ($e/grapheur/argument[@edit]) then
                 ( <info name="op">edit</info>,
                   <info name="opx">{
                       if ($e/grapheur/argument[@edit="true"]) then
                           "start"
                       else
                           "end" }</info>,
                   <info name="content"><text>{ $colorattr, 
                       if ($e/grapheur/argument[@edit="true"]) then
                           "d&#233;bute l'&#233;dition de"
                       else
                           "termine l'&#233;dition de",
                       local:objid(string($e/grapheur/argument/@id)) }</text></info> )
             else
             if ($e/grapheur/argument[@support]) then
                 (
                    <info name="op">{
                       if ($e/grapheur/argument[@support="true"]) then
                           ("support", local:objtype(string($e/grapheur/argument/@id)))
                       else if ($e/grapheur/argument[@support="none"]) then
                       	   ("neutral", local:objtype(string($e/grapheur/argument/@id)))
                       else
                           ("challenge", local:objtype(string($e/grapheur/argument/@id))) }
                    </info>,
                    <info name="opx">{
                       if ($e/grapheur/argument[@support="true"]) then
                           "yes"
                       else if ($e/grapheur/argument[@support="none"]) then
                       	   "none"
                       else
                           "no" }</info>,
                    <info name="content"><text>{ $colorattr,
                       if ($e/grapheur/argument[@support="true"]) then
                           "soutient"
                       else if ($e/grapheur/argument[@support="none"]) then
                       	   "est neutre sur"
                       else
                           "s'oppose &#224;",
                       local:objid(string($e/grapheur/argument/@id)) }</text></info> 
                 )
             else
             if ($e/grapheur/argument/name) then
                 ( <info name="op">{("change text",local:objtype(string($e/grapheur/argument/@id)))}</info>,
                   <info name="opx">argument</info>,
                   <info name="content"><text>{ $colorattr, 
                           "argument de",
                           local:objid(string($e/grapheur/argument/@id)),
                           ":",
                           string($e/grapheur/argument/name)}</text></info> )
             else
             if ($e/grapheur/argument/comment) then
                 ( <info name="op">{("change comment",local:objtype(string($e/grapheur/argument/@id)))}</info>,
                   <info name="opx">comment</info>,
                   <info name="content"><text>{ $colorattr, 
                           "commentaire de",
                           local:objid(string($e/grapheur/argument/@id)),
                           ":",
                           string($e/grapheur/argument/comment)}</text></info> )
             else
             if ($e/grapheur/argument/position) then
                 ( <info name="op">{("move",local:objtype(string($e/grapheur/argument/@id)))}</info>,
                   <info name="opx">{concat(($e/grapheur/argument/position/@x),",",
                         ($e/grapheur/argument/position/@y))}</info>,
                   <info name="content"><text>{ $colorattr, 
                       "d&#233;place en", concat(($e/grapheur/argument/position/@x),",",
                         ($e/grapheur/argument/position/@y)) ,
                       local:objid(string($e/grapheur/argument/@id)) }</text></info> )

             else
             if ($e/grapheur/argument/size) then
                 ( <info name="op">redimensionne</info>,
                   <info name="opx">{
                         concat(($e/grapheur/argument/size/@width),",",
                         ($e/grapheur/argument/size/@height))
                   }</info>,
                   <info name="content"><text>{ 
                       $colorattr, 
                       "nouvelle taille de ", 
                       local:objid(string($e/grapheur/argument/@id)),
				               ":",
				               concat(($e/grapheur/argument/size/@width),",",
                         ($e/grapheur/argument/size/@height))
                        }</text></info> 
                  )

	      else
                 (local:erreur("unmanaged grapher event", $e))
        )
    else
    if ($e/grapheur/relation) then
             if ($e/grapheur/relation[@action="create"]) then
                 if (fn:count($e/grapheur/relation[@action="create"])<2) then
                 ( <info name="op">relation</info>,
                   <info name="opx">create</info>,
                   <info name="content"><text>{ $colorattr,
                       "cr&#233;e une relation de" ,
                       local:objid(string($e/grapheur/relation/@from)) ,
                       "vers",
                       local:objid(string($e/grapheur/relation/@to)) }</text></info> )
                 else  
                 ( <info name="op">relation</info>,
                   <info name="opx">create</info>,
                   <info name="content"><text>{ $colorattr,
                       "cr&#233;e une relation de" ,
                       local:objid(string($e/grapheur/relation[1]/@to)) ,
                       "vers",
                       local:objid(string($e/grapheur/relation[2]/@to)) }</text></info> )   
             else
             if ($e/grapheur/relation[@action="delete"]) then
                 ( <info name="op">delete relation</info>,
                   <info name="opx">create</info>,
                   <info name="content"><text>{ $colorattr,
                       "efface la relation entre" ,
                       local:objid(string($e/grapheur/relation/@from)) ,
                       "et",
                       local:objid(string($e/grapheur/relation/@to)) }</text></info> )
             else
                 (local:erreur("unmanaged grapher relation event", $e))
    else
        (local:erreur("unmanaged grapher event", $e))
  }</item>

}</display>
